Repository navigation
Conversation
Rendering updates that Scheduler queues on RuntimeScheduler captured the SchedulerDelegate as a raw pointer. When a host detaches and destroys the delegate with no JS error involved (an iOS instance reload: RCTScheduler's dealloc calls setDelegate(nullptr) and frees SchedulerDelegateProxy), the old JS thread can still drain those updates in runEventLoopTick and call into the freed proxy. Queued updates now reach the delegate through a shared box guarded by a recursive mutex: an update calls the delegate it was queued for only while it is still the current one, and setDelegate waits for an update that is calling it. Fixes react#60324.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Fixes #60324.
Schedulerqueues rendering updates onRuntimeScheduler_Modern(fromuiManagerDidFinishTransaction,uiManagerDidDispatchCommandanduiManagerDidFinishReactCommit) that capture theSchedulerDelegateas a raw pointer. On iOS the delegate isSchedulerDelegateProxy, owned byRCTScheduler. When an instance is torn down for a reload,-[RCTScheduler dealloc]calls_scheduler->setDelegate(nullptr)and frees the proxy on the main thread, while the old JS thread can still be inrunEventLoopTick.updateRendering()then drains the queued lambda into freed memory, and the app crashes withEXC_BAD_ACCESSinRuntimeScheduler_Modern::runEventLoopTick. We see this in production afterexpo-updates'reloadAsync(); the stack trace is in the issue.This is the window that #58138 left open on purpose: a delegate detached with no JS error never reaches
handleTaskError, so nothing clears the queue. That PR notes that closing it needs more than the removed per-Schedulertoken. A token that is only flipped also leaves a check-then-call race, because the host can free the delegate while a lambda is between its check and its call.This change routes the queued updates through a
DelegateBox, shared bySchedulerand its lambdas and guarded by a mutex:box->delegateis still that delegate. A replaced or detached delegate is never called, which also keeps the old "updates go to the delegate they were queued for" behavior on a swap.setDelegateupdates the box under the same mutex, so it waits for a lambda that is currently calling the delegate. WhensetDelegate(nullptr)returns, the host can free the delegate (which is whatRCTSchedulerdoes).RCTSchedulerlocal in-[RCTSurfacePresenter _mergeReactRevisionForSurfaceId:]), which re-enterssetDelegateon the same thread.~Schedulerdeliberately does not take the mutex. Android destroys theSchedulerinsideuninstallFabricUIManagerwhile holdinginstallMutex_exclusively, and the delegate calls there take it shared (getMountingManager), so waiting there could deadlock. Only iOS callssetDelegate. On that path no lock the queued calls need is held:-[RCTSurfacePresenter suspend]holds_schedulerLifeCycleMutex, andscheduleTransaction/dispatchCommanddispatch to main asynchronously. Android's delegate is the binding itself and outlives theScheduler.The cost is one uncontended mutex lock per drained rendering update, on the JS thread.
Changelog:
[IOS] [FIXED] - Fix a crash (EXC_BAD_ACCESS in RuntimeScheduler_Modern::runEventLoopTick) when the React instance is reloaded while rendering updates are still queued
Test Plan:
SchedulerDelegateInvalidationTest, built and run on macOS with the OSS Fantom CMake build (./gradlew :private:react-native-fantom:configureFantomTester, thencmake --build ... --target react_renderer_scheduler_tests). It compiles with-Wall -Werror -Wpedantic.DelegateDetachedWithoutError_PendingRenderingUpdatesAreDropped). It queues updates from both the transaction and dispatch-command sites, detaches the delegate, drains, and expects no call.DelegateDetach_WaitsForRunningRenderingUpdate: while a queued update is inside the delegate call on one thread,setDelegate(nullptr)on another thread does not return until the call finishes.DelegateDetach_FromInsideRenderingUpdate: detaching from inside the delegate call on the JS thread doesn't deadlock, and the next queued update is dropped.Without the change (
Scheduler.cpp/.hreverted, tests kept), all three fail: the stale delegate is called,setDelegatereturns while the delegate runs (detached == true), and the second update calls the detached delegate (calls == 2). With the change, all 8 tests in the target pass, over three consecutive runs.